package com.wuercloud.mq.message.consumer;

import com.wuercloud.mq.common.ResultRes;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by JiangWL on 2017/3/30.
 */
@Component
public class ConsumerSubject {
    private List<ConsumerObserver> list = new ArrayList<ConsumerObserver>();
    /**
     * 业务系统初始化自己业务实现类时把业务实现ConsumerObserver类调用本方法注入进来
     * @param observer
     */
    public void attach(ConsumerObserver observer){
        list.add(observer);
        System.out.println("Attached an observer");
    }
    /**
     * 删除观察者对象
     * @param observer    观察者对象
     */
    public void detach(ConsumerObserver observer){
        list.remove(observer);
    }
    /**
     * 通知所有注册的观察者对象
     */
    public ResultRes nodifyObservers(final String bizQueue, final String message){
        ResultRes result = null;
        for(ConsumerObserver observer : list){
            if (observer.getBizQueue().equals(bizQueue)) {
                result = observer.consume(bizQueue,message);
                break;
            }
        }
        return result;
    }
}
